import { defineStore } from 'pinia';


export const usePLStore = defineStore({
  id: 'plStore',
  state: () => ({
    dragList: {},
    suffixUrl: null,
    readState: null,
    hasFocus: false,
    currentIndex: 0,
    currentPoint: null,
    currentImgId: null,
    loading: false
  }),
  getters: {
    getCurrentImgId() {
      return this.currentImgId
    },
    getCurrentPoint() {
      return this.currentPoint
    },
    getDragList() {
      return this.dragList
    },
    getSuffixUrl() {
      return this.suffixUrl || localStorage.getItem('suffixUrl')
    },
    getReadState() {
      return this.readState || localStorage.getItem('READ_STATE')
    },
    getHasFocus() {
      return this.hasFocus
    },
    getCurrentIndex() {
      return this.currentIndex
    },
    getLoading() {
      return this.loading
    }
  },
  actions: {
    setLoading(val) {
      this.loading = val
    },
    setCurrentImgId(id) {
      this.currentImgId = id
    },
    setCurrentPoint(point) {
      this.currentPoint = point
    },

    setCurrentIndex(i) {
      this.currentIndex = i
    },
    deleteLastDrag() {
      this.dragList[this.currentIndex].pop()
    },
    async setDragList(list) {
      await Object.values(this.dragList).forEach((v, i) => {
        if (i !== this.currentIndex) {
          v.forEach((t, ci) => {            
            if (t.sjtm === this.currentPoint!.id && (t.name === this.currentPoint!.name || t.name === this.currentPoint?.pName)) {
              v.splice(ci, 1)
            }
          })
        }
      })
      this.dragList[this.currentIndex] = list
    },
    resetDragList() {
      this.dragList = {}
    },
    setSuffixUrl(url) {
      this.suffixUrl = url
      localStorage.setItem('suffixUrl', url)
    },
    setReadState(val) {
      this.readState = val
      localStorage.setItem('READ_STATE', val)
    },
    setHasFocus(has) {
      this.hasFocus = has
    },
    initDrag(len) {
      Array.from(Array(len), (t, i) => i).forEach((v) => {
        this.dragList[v] = []
      })
      console.log(this.dragList);
    },
    // setIsCheck(val) {
    //   this.isCheck = val
    // }
  },
});
